package com.huangliusong.demohls;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;

public class speltest {
    public static void main(String[] args){

        ExpressionParser parser=new SpelExpressionParser();
        Expression expression=parser.parseExpression("'huangliusong'.bytes.length");
       Integer integer=(Integer) expression.getValue();
    System.err.println(integer);
        //String message=(String)expression.getValue();
        //System.err.println(message);
        test1();
        new speltest().test2();
        test3();
        test4();

    }

    public static void test1(){
        GregorianCalendar calendar=new GregorianCalendar();
        calendar.set(1856,7,9);
    }

    public  void test2(){
        Simple simple = new Simple();
        simple.booleanList.add(true);

        EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
        ExpressionParser parser=new SpelExpressionParser();

        parser.parseExpression("booleanList[0]").setValue(context, simple, "false");

        // b will be false
        Boolean b = simple.booleanList.get(0);
        System.err.println(b);
    }

    class Simple {
        public List<Boolean> booleanList = new ArrayList<Boolean>();
    }

    public static  void test3(){
        ExpressionParser parser=new SpelExpressionParser();
        Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class);

        Class stringClass = parser.parseExpression("T(String)").getValue(Class.class);

        boolean trueValue = parser.parseExpression(
                "T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR")
                .getValue(Boolean.class);
        System.err.println(dateClass);
        System.err.println(stringClass);
        System.err.println(trueValue);

    }

    public static  void test4(){
        List<Integer> primes=new ArrayList<Integer>();
        primes.addAll(Arrays.asList(11,22,23,4,25,26,7,8,9,0));

        ExpressionParser parser=new SpelExpressionParser();
        EvaluationContext context=SimpleEvaluationContext.forReadOnlyDataBinding().build();
        context.setVariable("primes",primes);

        List<Integer> primesGraderThanTen=(List<Integer>)parser.parseExpression("#primes.?[#this>=0]").getValue(context);
        System.err.println(primesGraderThanTen);



    }


}
